package com.system.healthproject.model.sickuser.service.imp;

import com.system.healthproject.bean.SickOneIsLivePatientActivity;
import com.system.healthproject.bean.SickOnePatientActivityBean;
import com.system.healthproject.bean.SickSignUpPatientActivityBean;
import com.system.healthproject.bean.SickUpPatientActivityBean;
import com.system.healthproject.mapper.LoginMapper;
import com.system.healthproject.mapper.PatientActivityMapper;
import com.system.healthproject.mapper.PatientActivitySignUpMapper;
import com.system.healthproject.model.sickuser.service.SickPatientActivityService;
import com.system.healthproject.pojo.PatientActivity;
import com.system.healthproject.pojo.PatientActivitySignUp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Xuheng Zhou
 * @date 2020/6/14 15:44
 * @desc
 */

@Service
@Transactional
public class SickPatientActivityServiceImpl implements SickPatientActivityService {

    @Autowired
    private PatientActivityMapper mapper;

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private PatientActivitySignUpMapper signUpMapper;


    @Override
    public Integer selectPatientIdByLoginId(Integer loginId) {
        return loginMapper.selectPatientIdByLoginId(loginId);
    }

    @Override
    public List<SickUpPatientActivityBean> selectPatientIdPatientActivity(Integer patientId, Integer page, Integer size) {
        return mapper.selectPatientIdPatientActivity(patientId,(page-1)*size,size);
    }

    @Override
    public List<SickSignUpPatientActivityBean> selectPatientIdSignUpPatientActivity(Integer patientId, Integer page, Integer size) {
        return mapper.selectPatientIdSignUpPatientActivity(patientId,(page-1)*size,size);
    }

    @Override
    public SickOnePatientActivityBean selectSickOnePatientActivity(Integer patientactivityId) {
        return mapper.selectSickOnePatientActivity(patientactivityId);
    }

    @Override
    public boolean SickSignUpPatientActivity(Integer patientId,Integer  patientActivityId ) {

        boolean tf=false;
        //查询该活动的目标人数
        Integer peopleNum=mapper.selectPatientActivityById(patientActivityId).getPeoplenumber();
        //查询该活动的报名人数
        Integer SignUpCount=signUpMapper.selectSignUpPerson(patientActivityId);

        if ( SignUpCount<=peopleNum){
            PatientActivitySignUp patientActivitySignUp=new PatientActivitySignUp();
            patientActivitySignUp.setPatientId(patientId);
            patientActivitySignUp.setPatientactivityId(patientActivityId);
            patientActivitySignUp.setCratetime(new Date());
            tf=signUpMapper.insertSelective(patientActivitySignUp)>0?true:false;
        }
        return tf;
    }

    @Override
    public SickOneIsLivePatientActivity selectSickOneIsLivePatientActivity(Integer patientactivityId) {
        return mapper.selectSickOneIsLivePatientActivity(patientactivityId);
    }

    @Override
    public PatientActivitySignUp selectPatientActivitySignUpIsNull(Integer patientactivityId, Integer patientId) {
        return signUpMapper.selectPatientActivitySignUpIsNull(patientactivityId,patientId);
    }

    @Override
    public PatientActivity checkPatientActivity(Integer patientactivityId) {
        return mapper.selectByPrimaryKey(patientactivityId);
    }
}
